package com.hongyi.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hongyi.common.framework.constant.UserConstans;
import com.hongyi.common.framework.exception.ServiceException;
import com.hongyi.common.framework.utils.bean.MapstructUtils;
import com.hongyi.common.framework.utils.string.StringUtils;
import com.hongyi.common.mybatis.page.PageData;
import com.hongyi.common.mybatis.page.PageQuery;
import com.hongyi.system.domain.SysUserPost;
import com.hongyi.system.domain.dto.SysPostDto;
import com.hongyi.system.domain.vo.SysPostVo;
import com.hongyi.system.mapper.SysUserPostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongyi.system.domain.SysPost;
import com.hongyi.system.mapper.SysPostMapper;
import com.hongyi.system.service.ISysPostService;

import java.util.Arrays;
import java.util.List;

@Service
public class SysPostServiceImpl implements ISysPostService {

    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    /**
     * 查询条件构造器
     *
     * @param sysPostDto 查询条件
     * @return 查询条件构造器
     */
    private LambdaQueryWrapper<SysPost> buildQueryWrapper(SysPostDto sysPostDto) {
        LambdaQueryWrapper<SysPost> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(sysPostDto.getPostCode()), SysPost::getPostCode, sysPostDto.getPostCode());
        lqw.like(StringUtils.isNotBlank(sysPostDto.getPostName()), SysPost::getPostName, sysPostDto.getPostName());
        lqw.eq(StringUtils.isNotBlank(sysPostDto.getStatus()), SysPost::getStatus, sysPostDto.getStatus());
        lqw.orderByAsc(SysPost::getPostSort);
        return lqw;
    }

    /**
     * 获取岗位列表
     *
     * @param sysPostDto 查询条件
     * @param pageQuery 分页信息
     * @return 岗位列表分页数据
     */
    @Override
    public PageData<SysPostVo> selectPagePostList(SysPostDto sysPostDto, PageQuery pageQuery) {
        LambdaQueryWrapper<SysPost> lqw = buildQueryWrapper(sysPostDto);
        IPage<SysPostVo> sysPostVoResult = sysPostMapper.selectVoPage(pageQuery.build(), lqw);
        return PageData.success(sysPostVoResult);
    }

    @Override
    public List<SysPostVo> selectPostList(SysPostDto sysPostDto) {
        LambdaQueryWrapper<SysPost> lqw = buildQueryWrapper(sysPostDto);
        return sysPostMapper.selectVoList(lqw);
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Long postId) {
        return sysPostMapper.selectById(postId);
    }

    /**
     * 校验岗位名称
     *
     * @param sysPostDto 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostNameUnique(SysPostDto sysPostDto) {
        boolean exist = sysPostMapper.exists(new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getPostName, sysPostDto.getPostName())
                .ne(ObjectUtil.isNotNull(sysPostDto.getPostId()), SysPost::getPostId, sysPostDto.getPostId()));
        return exist? UserConstans.NOT_UNIQUE:UserConstans.UNIQUE;
    }

    /**
     * 校验岗位编码
     *
     * @param sysPostDto 岗位信息
     * @return 结果
     */
    @Override
    public String checkPostCodeUnique(SysPostDto sysPostDto) {
        boolean exist = sysPostMapper.exists(new LambdaQueryWrapper<SysPost>()
                .eq(SysPost::getPostCode, sysPostDto.getPostCode())
                .ne(ObjectUtil.isNotNull(sysPostDto.getPostId()), SysPost::getPostId, sysPostDto.getPostId()));
        return exist? UserConstans.NOT_UNIQUE:UserConstans.UNIQUE;
    }

    /**
     * 新增保存岗位信息
     *
     * @param sysPostDto 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(SysPostDto sysPostDto) {
        SysPost sysPost = MapstructUtils.convert(sysPostDto, SysPost.class);
        return sysPostMapper.insert(sysPost);
    }

    /**
     * 修改保存岗位信息
     *
     * @param sysPostDto 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(SysPostDto sysPostDto) {
        SysPost sysPost = MapstructUtils.convert(sysPostDto, SysPost.class);
        return sysPostMapper.updateById(sysPost);
    }

    @Override
    public List<Long> selectPostListByUserId(Long userId) {
        return sysPostMapper.selectPostListByUserId(userId);
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public long countUserPostById(Long postId) {
        return sysUserPostMapper.selectCount(new LambdaQueryWrapper<SysUserPost>().eq(SysUserPost::getPostId, postId));
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deletePostByIds(Long[] postIds) {
        for (Long postId : postIds) {
            SysPost sysPost = sysPostMapper.selectById(postId);
            if (countUserPostById(postId) > 0) {
                throw new ServiceException(String.format("%1$s已分配，不能删除!", sysPost.getPostName()));
            }
        }
        return sysPostMapper.deleteBatchIds(Arrays.asList(postIds));
    }


}
